; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -loop-idiom -S %s | FileCheck %s

define void @phi_insertion(i1 %c, i32* %ptr.start, i32* %ptr.end, i64 %offset) {
; CHECK-LABEL: @phi_insertion(
; CHECK-NEXT:  bb5:
; CHECK-NEXT:    br i1 [[C:%.*]], label [[LOOP_0_PH:%.*]], label [[EXIT:%.*]]
; CHECK:       loop.0.ph:
; CHECK-NEXT:    br label [[LOOP_0:%.*]]
; CHECK:       loop.0:
; CHECK-NEXT:    [[IV_0:%.*]] = phi i32* [ [[IV_0_NEXT:%.*]], [[LOOP_0_LATCH:%.*]] ], [ [[PTR_START:%.*]], [[LOOP_0_PH]] ]
; CHECK-NEXT:    [[COND:%.*]] = icmp eq i32* [[IV_0]], [[PTR_END:%.*]]
; CHECK-NEXT:    br i1 [[COND]], label [[LOOP_1_PREHEADER:%.*]], label [[EXIT_LOOPEXIT:%.*]]
; CHECK:       loop.1.preheader:
; CHECK-NEXT:    br label [[LOOP_1:%.*]]
; CHECK:       loop.1:
; CHECK-NEXT:    [[PTR_1:%.*]] = getelementptr inbounds i32, i32* [[IV_0]], i64 0
; CHECK-NEXT:    [[LV:%.*]] = load i32, i32* [[PTR_1]], align 8
; CHECK-NEXT:    [[PTR_2:%.*]] = getelementptr inbounds i32, i32* [[IV_0]], i64 0
; CHECK-NEXT:    store i32 [[LV]], i32* [[PTR_2]], align 8
; CHECK-NEXT:    [[IV_1_NEXT:%.*]] = add nuw nsw i64 0, 1
; CHECK-NEXT:    [[EC:%.*]] = icmp eq i64 [[IV_1_NEXT]], 100
; CHECK-NEXT:    br i1 [[EC]], label [[LOOP_0_LATCH]], label [[LOOP_1]]
; CHECK:       loop.0.latch:
; CHECK-NEXT:    [[IV_0_NEXT]] = getelementptr inbounds i32, i32* [[IV_0]], i64 [[OFFSET:%.*]]
; CHECK-NEXT:    br label [[LOOP_0]]
; CHECK:       exit.loopexit:
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    ret void
;
bb5:
  br i1 %c, label %loop.0.ph, label %exit

loop.0.ph:                                        ; preds = %bb5
  br label %loop.0

loop.0:                                           ; preds = %loop.0.latch, %loop.0.ph
  %iv.0 = phi i32* [ %iv.0.next, %loop.0.latch ], [ %ptr.start, %loop.0.ph ]
  %cond = icmp eq i32* %iv.0, %ptr.end
  br i1 %cond, label %loop.1, label %exit

loop.1:                                           ; preds = %loop.1, %loop.0
  %iv.1 = phi i64 [ 0, %loop.0 ], [ %iv.1, %loop.1 ]
  %ptr.1 = getelementptr inbounds i32, i32* %iv.0, i64 %iv.1
  %lv = load i32, i32* %ptr.1, align 8
  %ptr.2 = getelementptr inbounds i32, i32* %iv.0, i64 %iv.1
  store i32 %lv, i32* %ptr.2, align 8
  %iv.1.next = add nuw nsw i64 %iv.1, 1
  %ec = icmp eq i64 %iv.1.next, 100
  br i1 %ec, label %loop.0.latch, label %loop.1

loop.0.latch:                                     ; preds = %loop.1
  %iv.0.next = getelementptr inbounds i32, i32* %iv.0, i64 %offset
  br label %loop.0

exit:                                             ; preds = %loop.0, %bb5
  ret void
}
